package com.mzw.huaxiarealestatebackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.mzw.huaxiarealestatebackend.common.utils.DataResult;
import com.mzw.huaxiarealestatebackend.common.utils.DateFormat;
import com.mzw.huaxiarealestatebackend.dao.UserDao;
import com.mzw.huaxiarealestatebackend.entity.*;
import com.mzw.huaxiarealestatebackend.service.*;
import io.swagger.models.auth.In;
import org.apache.commons.collections.ResettableListIterator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (Project)表控制层
 *
 * @author makejava
 * @since 2023-12-22 00:02:59
 */
@RestController
@RequestMapping("project")
public class ProjectController {
    /**
     * 服务对象
     */
    @Resource
    private ProjectService projectService;
    @Autowired
    private MailService mailService;
    @Autowired
    private ClientService clientService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private ProjectRecordService projectRecordService;

    /**
     * 建模主管审核项目资料
     */
    @PutMapping("/checkDocument")
    public DataResult checkDocument(@RequestParam("userId") String userId,@RequestParam("pjtNo") String pjtNo,@RequestParam("isAccepted") Integer isAccespted,@RequestParam("reason")  String reason){
        //查找当前项目处于建项阶段的代码
        ProjectRecord pr = new ProjectRecord();
        pr.setFileStatus("1");
        pr.setPjtNo(pjtNo);
        List<ProjectRecord> projectRecords = this.projectRecordService.queryByCondition(pr);
        //如果找不到一条记录，则是直接退出
        if (projectRecords.size() == 0){
            return DataResult.failure("审核失败");
        }
        //当前项目记录
        ProjectRecord projectRecord = projectRecords.get(0);
        //当前项目
        Project project1 = this.projectService.queryById(pjtNo);
        if (isAccespted == 1){
            //更新项目信息
            project1.setPjtStop("2");
            this.projectService.update(project1);
            //更新项目记录信息
            projectRecord.setPrcdMcheckstatus(1);
            projectRecord.setPrcdFinishstatus(1);
            projectRecord.setPrcdCtime(DateFormat.getNowDateFormat());
            this.projectRecordService.update(projectRecord);
        }else {
            //如果是不同意建立文件夹，则是向用户发送消息
            project1.setPjtStop("1");
            Staff staff = this.staffService.queryById(userId);
            String cltNo = project1.getCltNo();
            this.mailService.sendMail(userId, cltNo, staff.getStfName(), reason);
        }
        return DataResult.ok();
    }

//    /**
//     * 根据员工id展示项目
//     * （只展示需要完成当前阶段的项目）
//     * 比如说建模主管就只可以查看已经完成了前期准备工作的项目
//     */
//    @GetMapping("/queryByUserId")
//    public DataResult queryByUserId(@RequestParam("userId") String userId){
//        Staff staff = this.staffService.queryById(userId);
//        Project project = new Project();
//        String dpNo = staff.getDpNo();
//        List<Project> projects;
//        if (!dpNo.equals("1")){
//            project.setPjtStop(dpNo);
//            projects = this.projectService.queryByCondition(project);
//        }else {
//            projects = this.projectService.queryAll();
//        }
//
//        //加上中文,将代号转化为具体的信息
//        for (Project p:
//             projects) {
//            Client client = this.clientService.queryById(p.getCltNo());
//            p.resetName(client.getCltName());
//        }
//        return DataResult.ok(projects);
//    }
    /**
     * 根据员工id展示项目
     * （只展示未完成当前阶段的项目）
     * 比如说建模主管就只可以查看已经完成了前期准备工作的项目
     */
    @GetMapping("/queryByUserIdUncompleted")
    public DataResult queryByUserIdUncompleted(@RequestParam("userId") String userId){
        Staff staff = this.staffService.queryById(userId);
        String dpNo = staff.getDpNo();
        Project project = new Project();
        List<Project> projects = new ArrayList<>();
        if (!dpNo.equals("1")){
            if (dpNo.equals("2")){
                project.setPjtStop("1");
                projects.addAll(this.projectService.queryByCondition(project));
                project.setPjtStop("2");
                projects.addAll(this.projectService.queryByCondition(project));
            }else {
                project.setPjtStop(dpNo);
                projects = this.projectService.queryByCondition(project);
            }
        }else {
            projects = this.projectService.queryAll();
        }


        //加上中文,将代号转化为具体的信息
        for (Project p:
                projects) {
            Client client = this.clientService.queryById(p.getCltNo());
            p.resetName(client.getCltName());
        }
        return DataResult.ok(projects);
    }



    /**
     * 根据员工id展示项目
     * （只展示需要完成当前阶段的项目）
     *
     */
    @GetMapping("/queryByUserIdCompleted")
    public DataResult queryByUserIdCompleted(@RequestParam("userId") String userId){
        Staff staff = this.staffService.queryById(userId);
        String dpNo = staff.getDpNo();
        Project project = new Project();
        List<Project> projects = new ArrayList<>();
        if (!dpNo.equals("1")){
            int phase = Integer.parseInt(dpNo);
            for(int i = phase; i < 6; i++){
                project.setPjtStop(Integer.toString(i + 1));
                projects.addAll(projectService.queryByCondition(project));
            }
        }else {
            projects = this.projectService.queryAll();
        }
        //加上中文,将代号转化为具体的信息
        for (Project p:
                projects) {
            Client client = this.clientService.queryById(p.getCltNo());
            p.resetName(client.getCltName());
        }
        return DataResult.ok(projects);
    }


    /**
     * 根据客户id展示项目
     * （只展示未完成当前阶段的项目）
     * 比如说建模主管就只可以查看已经完成了前期准备工作的项目
     */
    @GetMapping("/queryByClientId")
    public DataResult queryByClientId(@RequestParam("userId") String userId){
        Project project = new Project();
        project.setCltNo(userId);
        List<Project> projects = this.projectService.queryByCondition(project);
        //加上中文
        for (Project p:
                projects) {
            Client client = this.clientService.queryById(p.getCltNo());
            p.resetName(client.getCltName());
        }
        return DataResult.ok(projects);
    }


    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public DataResult queryAll(){
        List<Project> projects = this.projectService.queryAll();
        //加上中文
        for (Project p:
                projects) {
            Client client = this.clientService.queryById(p.getCltNo());
            p.resetName(client.getCltName());
        }
        return DataResult.ok(projects);
    }


    /**
     * 新添加项目
     * @return
     */
    @PostMapping("/addProject")
    public DataResult buildProject(@RequestBody Project project){
        project.setPjtBuildtime(DateFormat.getNowDateFormat());
        project.setPjtStatus("0");
        project.setPjtStop("1");
        boolean insert = this.projectService.insert(project);
        if (insert) {
            ProjectRecord projectRecord = new ProjectRecord();
            projectRecord.setPjtNo(project.getPjtNo());
            projectRecord.setPrcdTime(DateFormat.getNowDateFormat());
            projectRecord.setPcsNo(1);
            projectRecord.setPrcdCcheckstatus(1);
            boolean insert1 = this.projectRecordService.insert(projectRecord);
            if (insert1) return DataResult.ok("添加成功");
        }
        return DataResult.failure("添加失败");
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public DataResult queryById(@PathVariable("id") String id) {
        Project project = this.projectService.queryById(id);
        if (project != null){
            Client client = this.clientService.queryById(project.getCltNo());
            project.resetName(client.getCltName());
            return DataResult.ok(project);
        }
        return DataResult.failure("项目不存在");
    }


    /**
     * 编辑数据
     *
     * @param project 实体
     * @return 编辑结果
     */
    @PutMapping
    public DataResult edit(@RequestBody Project project) {
        boolean update = this.projectService.update(project);
        if (update){
            return DataResult.ok("修改成功");
        }
        return DataResult.failure("修改失败");
    }


}
